var express = require('express');
var router = express.Router();
var { User, Run, Coordinate } = require('../model/model')


//添加跑步记录
router.post("/addRun", async (req, res) => {
  await Run.create(req.body)
  //将新添加的跑步记录_id返回给前端
  let run = await Run.findOne(req.body)
  console.log(run);
  res.send({
    code: 200,
    msg: '添加成功',
  })
})
//修改跑步记录
router.put("/updateRun", async (req, res) => {
  //获取前端传递的id
  let _id = req.body.id
  //根据id修改跑步记录
  await Run.updateOne({ _id: _id }, req.body)
  res.send({
    code: 200,
    msg: '修改成功',
  })
})

//获取跑步记录
router.get("/getRun", async (req, res) => {
  let title = req.query.title
  //判断用户是否传递了标题
  if (!title) {
    //获取所有跑步记录
    let runs = await Run.find()
    res.send({
      code: 200,
      msg: '获取成功',
      runs
    })
  } else {
    //根据用户传递的标题获取跑步记录
    let runs = await Run.find({ title: title })
    res.send({
      code: 200,
      msg: '获取成功',
      runs
    })
  }
})
//获取坐标
router.get("/getzb", async (req, res) => {
  let { run } = req.query
  let zb = await Coordinate.find({ run: run })
  res.send({
    code: 200,
    msg: "获取成功",
    zb
  })
})
//添加坐标
router.post("/addzb", async (req, res) => {
  //添加坐标
  await Coordinate.create(req.body)
  res.send({
    code: 200,
    msg: "添加成功",
  })
})



module.exports = router;



